<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>

<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="" lay-verify="required" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">工号</label>
                        <div class="layui-input-block">
                            <input type="text" name="jobNumber" value="" lay-verify="required" autocomplete="off"
                                class="layui-input" disabled>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="userName" value="" lay-verify="required" autocomplete="off"
                                placeholder="请输入姓名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="1" title="男" checked>
                            <input type="radio" name="sex" value="0" title="女">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">职位</label>
                        <div class="layui-input-block">
                            <input type="text" name="job" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入职位" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">部门</label>
                        <div class="layui-input-block">
                            <input type="text" name="dept" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入部门" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" value="" lay-verify="title" autocomplete="off"
                                placeholder="请输入电话" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="password" value="" autocomplete="off" placeholder="请输入密码"
                                class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>

    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;


            var obj = localStorage.getItem("token");
            const base64Url = obj.split('.')[1];
            const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
            const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
            const decodedToken = JSON.parse(jsonPayload);
       
            var id = decodedToken.id;

            // 页面加载时向后端发送请求获取需要回显的数据
            $.ajax({
                url: 'http://localhost:8080/user/query?id=' + id,
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
                success: function (res) {
                    $("input[name='id']").val(res.data[0].id);
                    $("input[name='jobNumber']").val(res.data[0].jobNumber);
                    $("input[name='userName']").val(res.data[0].userName);
                    $("input[name='sex'][value='" + res.data[0].sex + "']").prop('checked', true);
                    $("input[name='job']").val(res.data[0].job);
                    $("input[name='dept']").val(res.data[0].dept);
                    $("input[name='phone']").val(res.data[0].phone);
                }
            });

            form.on('submit(user-save)', function (data) {
                let roleIds = "";
                $('input[type=checkbox]:checked').each(function () {
                    roleIds += $(this).val() + ",";
                });
                roleIds = roleIds.substr(0, roleIds.length - 1);
                data.field.roleIds = roleIds;


                $.ajax({
                    url: 'http://localhost:8080/user/update',
                    data: data.field,
                    type: 'post',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('token', localStorage.getItem("token"));
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>